<template>
  <div class="list">
    <div class="content">
      <div class="crumbs">
        <a @click="$router.push('/')" class="item-a">首页</a>
        <span class="arrow">></span>
        <span class="item-span">特色农产品列表</span>
      </div>
      <div class="goods-list">
        <div class="list-con" >
          <div class="item" v-for="item in charProductData" :key="item._id" @click="goDetail(item._id)">
            <div class="goods-img">
              <img :src="item.productImage" alt="" />
            </div>
            <div class="goods-msg">
              <div class="goods-name">{{ item.productName }}</div>
              <div class="label">
                <div class="tag" v-for="tag in item.label">{{ tag.name }}</div>
              </div>
              <div class="goods-price">
                <div class="price">
                  ￥<span class="medium">{{ item.price }}</span>
                </div>
              </div>
            </div>
          </div>
        </div>
        <div class="pagination">
          <el-pagination
            background
            v-model:current-page="pageNo"
            v-model:page-size="pageSize"
            v-model:total="total"
            layout="prev, pager, next"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts" name="list">
import { ref, onMounted } from "vue";
import { useRouter, useRoute } from "vue-router";
import { reqAllCharProductList } from "@/api/charProduct";

//获取路由器对象
let $router = useRouter();
let $route = useRoute();
// 分页器数据
let pageNo = ref(1);
let pageSize = ref(10);
let total = ref(0);
// 农产品数据
let charProductData = ref<any>([]);

// 点击跳转到商品详情页
const goDetail = (_id:string) => {
  $router.push({
    path:`/detail/${_id}`,
    query:{
      charProduct:1
    }
  });
};
// 获取特色农产品列表
const getCharProductList = async () => {
  let result = await reqAllCharProductList(pageNo.value, pageSize.value);
  if (result.code == 200) {
    charProductData.value = result.data;
    total.value = result.total;
  }
};

// 分页器回调
const handleSizeChange = (value: number) => {
  pageNo.value = value;
};
const handleCurrentChange = (value: number) => {
  pageNo.value = value;
};

onMounted(() => {
  getCharProductList();
});
</script>

<style lang="scss" scoped>
.list {
  padding-bottom: 40px;
  min-height: 535px;
  background: #f5f5f5;
  .content {
    width: 1200px;
    margin: auto;
    .crumbs {
      font-size: 12px;
      line-height: 1;
      padding: 23px 0 20px;
      .item-a {
        cursor: pointer;
        &:hover {
          text-decoration: underline;
        }
      }
      .arrow {
        font-family: Consolas;
        margin: 0 2px;
      }
      .item-span {
        color: #999;
      }
    }
    .goods-list {
      padding-bottom: 30px;
      .list-con {
        display: flex;
        flex-wrap: wrap;
        min-height: 440px;
        .item {
          position: relative;
          width: 224px;
          height: 298px;
          background: #fff;
          margin-top: 20px;
          margin-right: 20px;
          transition: all 0.2s;
          cursor: pointer;
          &:hover {
            box-shadow: 0 0 16px 0 hsla(0, 0%, 60%, 0.2);
          }
          &:nth-child(5n) {
            margin-right: 0;
          }
          .goods-img {
            width: 160px;
            height: 160px;
            margin: 18px 32px 15px;
            text-align: center;
            font-size: 0;
            img {
              width: 160px;
              height: 160px;
              vertical-align: middle;
            }
          }
          .goods-msg {
            padding: 0 23px 10px;
            .goods-name {
              height: 40px;
              line-height: 20px;
              font-size: 14px;
              max-width: 100%;
              overflow: hidden;
              text-overflow: ellipsis;
              display: -webkit-box;
              -webkit-box-orient: vertical;
              -webkit-line-clamp: 2;
            }
            .label {
              margin-top: 4px;
              overflow: hidden;
              display: flex;
              flex-wrap: wrap;
              width: 100%;
              .tag {
                margin-left: 6px;
                height: 18px;
                padding-left: 4px;
                padding-right: 4px;
                font-family: PingFangSC-Regular;
                font-size: 11px;
                display: flex;
                flex: none;
                align-items: center;
                background: #eff4fb;
                border: 1px solid #8a9fcc;
                color: #4869af;
                border-radius: 4px;
                &:first-child {
                  margin-left: 0;
                }
              }
            }
            .goods-price {
              display: flex;
              margin-top: 10px;
              overflow: hidden;
              .price {
                font-family: arial;
                font-size: 12px;
                color: #e1251b;
                display: flex;
                align-items: baseline;
                .medium {
                  font-size: 22px;
                }
              }
            }
          }
        }
      }
    }
    .pagination {
      margin-top: 30px;
      display: flex;
      justify-content: flex-end;
      text-align: center;
    }
  }
}
</style>
